package cnpm02.j2ee.group3.logic.model;

import java.util.Calendar;
import java.util.Date;

import javax.faces.model.SelectItem;

import org.jboss.seam.Component;

import cnpm02.j2ee.group3.logic.utils.ATSMUtils;
import cnpm02.j2ee.group3.model.City;
import cnpm02.j2ee.group3.model.Ticket;

/**
 * Contain suitable data for the UI (xhtml) uses
 * @author BINH
 * @version 1.0 
 * */
public class TicketDTO {
	
	/**
	 * Use for tracing which TicketDTO is selected
	 * */
	private String key; 
	
	private SelectItem bussinessFlex;
	private SelectItem economicFlex;
	private SelectItem saverFlex;
	private SelectItem saver;
	
	
	private City from;
	private City to;
	private String fromCode;
	private String toCode;
	private String arriveTime;
	private String plane;
	private Date departDayAndDate;
	private Date arriveDayAndDate;
	
	private ATSMUtils atsmUtils;
	
	public TicketDTO(String key) {
		atsmUtils = (ATSMUtils) Component.getInstance("atsmUtils");
		this.key = key;
	}
	
	/**
	 * Add ticket
	 * @param ticket
	 * @throws Exception throw when the entity which need translating is not translatable
	 * */
	public void addTicket(Ticket ticket) throws Exception {
		switch (ticket.getTicketType().getCode()) {
		case PHT1:
			this.bussinessFlex = new SelectItem(key,
					atsmUtils.formatMoney(ticket.getPrice()));
			break;
		case THG1:
			this.economicFlex = new SelectItem(key,
					atsmUtils.formatMoney(ticket.getPrice()));
			break;
		case TKI1:
			this.saverFlex = new SelectItem(key, atsmUtils.formatMoney(ticket
					.getPrice()));
			break;
		case TKI2:
			this.saver = new SelectItem(key, atsmUtils.formatMoney(ticket
					.getPrice()));
			break;
		}
		
		if (arriveTime == null) {
			arriveTime = atsmUtils.stringOf(ticket.getFlight()
					.getArriveDate(), "hh:mm");
		}
		if (from == null) {
			from = ticket.getFlight().getFlightLine().getFrom();
		}
		if(to == null) {
			to = ticket.getFlight().getFlightLine().getTo();
		}
		if(fromCode == null) {
			fromCode = ticket.getFlight().getFlightLine()
			.getFrom().getCode();
		}
		if(toCode == null) {
			toCode = ticket.getFlight().getFlightLine()
			.getTo().getCode();
		}
		if(plane == null) {
			plane = ticket.getFlight().getPlane().getPlaneName();
		}
		if(departDayAndDate == null) {
			departDayAndDate = ticket.getFlight().getDepartDate();
		}
		if(arriveDayAndDate == null) {
			arriveDayAndDate = ticket.getFlight().getArriveDate();
		}
	}
	
	public SelectItem getBussinessFlex() {
		return bussinessFlex;
	}
	
	public SelectItem getEconomicFlex() {
		return economicFlex;
	}
	
	public SelectItem getSaverFlex() {
		return saverFlex;
	}
	
	public SelectItem getSaver() {
		return saver;
	}

	public String getArriveTime() {
		return arriveTime;
	}

	public String getDepartTime() {
		return atsmUtils.stringOf(departDayAndDate, "hh:mm");
	}
	
	public String getFrom() throws Exception {
		return atsmUtils.translate(from);
	}

	public String getTo() throws Exception {
		return atsmUtils.translate(to);
	}

	public String getPlane() {
		return plane;
	}
	
	public String getFromCode() {
		return fromCode;
	}

	public String getToCode() {
		return toCode;
	}
	
	private String convertDateToString(Date d) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		String day = atsmUtils.getDayOfWeek(cal.get(Calendar.DAY_OF_WEEK));
		String date = atsmUtils.getDateExcludeYear(cal.getTime());
		return day + ", " + date;
	}
	
	public String getDepartDayAndDate() {
		return convertDateToString(departDayAndDate);
	}
	
	@Override
	public String toString() {
		return "TicketDTO";
	}

	public String getArriveDayAndDate() {
		return convertDateToString(arriveDayAndDate);
	}

}
